使用launchd运行的Offlineimap无法找到用于隐藏密码的python模块

时间:2016-05-01 17:54:46

标签: python macos imap launchd

我正试图让offlineimap使用launchd在OSX El Capitan的后台运行。

这是我的plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.andypierz.offlineimap.plist</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/offlineimap</string>
    <string>-u</string>
    <string>quiet</string>
   </array>
   <key>StartInterval</key>
   <integer>60</integer>
   <key>StandardErrorPath</key>
     <string>/Users/Andy/.Mailder/offlineimap_err.log</string>
    <key>StandardOutPath</key>
      <string>/Users/Andy/.Mailder/offlineimap.log</string>
</dict>
</plist>

这会加载并运行,但是我的日志显示offlineimap正在运行错误:

OfflineIMAP 6.7.0
  Licensed under the GNU GPL v2 or any later version (with an OpenSSL exception)
ERROR: No module named keyring
ERROR: Exceptions occurred during the run!
ERROR: No module named keyring

为了避免在我的.offlineimaprc中使用明文密码,我使用了here描述的python密钥环方法。

当我从终端运行offlineimap它工作正常,我能够使用python密钥环导入我的密码没有问题。同样,当我将offlineimap作为cronjob运行时,这似乎也没有发生任何事故。但是,在OSX上不推荐使用cron,所以我更喜欢使用launchd。

我的.offlineimaprc的相关部分:

[general]
accounts = personal, work
maxsyncaccounts = 3
pythonfile = /Users/Andy/offlineimap.py

[Repository personalRemote]
type = IMAP
remotehost = myhost.com
remoteuser = myalperson@email.com
remotepasseval = keyring.get_password('email', 'personal')

[Repository workRemote]
type = IMAP
remotehost = myhost.com
remoteuser = mywork@email.com
remotepasseval = keyring.get_password('email', 'work')

我的offlineimap.py文件只是

#!/usr/bin/python
import keyring

1 个答案:

答案 0 :(得分:1)

回答错误:

$sudo pip install keyring

如果这不适合你,

0)在钥匙串中创建密码,如完成here in Retrieving Passwords section

1)创建文件

$touch ~/offlineimap.py

<强> offlineimap.py

#!/usr/bin/python
import re, subprocess
def get_keychain_pass(account=None, server=None):
    params = {
        'security': '/usr/bin/security',
        'command': 'find-internet-password',
        'account': account,
        'server': server,
        'keychain': '/Users/${USER}/Library/Keychains/login.keychain',
    }
    command = "sudo -u ${USER} %(security)s -v %(command)s -g -a %(account)s -s %(server)s %(keychain)s" % params
    output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
    outtext = [l for l in output.splitlines()
               if l.startswith('password: ')][0]

    return re.match(r'password: "(.*)"', outtext).group(1)

2)编辑

中的行

<强> .offlineimaprc

pythonfile = ~/offlineimap.py

3)并更新远程部分

remotepasseval = get_keychain_pass(account="testUser@company.com", server="sub.domain.com")

4)如果仍有问题,请尝试将 offlineimap.py 文件中的命令参数更改为

find-generic-password

5)更正标签字符串并在启动期间运行一次

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.andypierz.offlineimap</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
    <string>sh</string>
    <string>-c</string>
    <string>/usr/local/bin/offlineimap -u quiet</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

6)加载它

launchctl load com.andypierz.offlineimap.plist