├── .gitignore ├── LICENSE ├── README.md ├── deploycron └── __init__.py ├── setup.py └── tests └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 CHAO LIU 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deploycron 2 | 3 | A small tool for deploying crontab into your system. 4 | 5 | It's useful if you want to deploy some crontab scripts into your system when you deploy your software that contains some extra crontab scripts. 6 | 7 | # Install 8 | 9 | ```bash 10 | pip install deploycron 11 | ``` 12 | 13 | # Usage 14 | 15 | There's only one function in the package now, 16 | 17 | ```python 18 | def deploycron(filename="", content="", override=False): 19 | ``` 20 | 21 | > Install crontabs into the system if it's not installed. 22 | > This will not remove the other crontabs installed in the system if not specified 23 | > as override. It just merge the new one with the existing one. 24 | > If you provide `filename`, then will install the crontabs in that file 25 | > otherwise install crontabs specified in content 26 | > 27 | > `filename` - file contains crontab, one crontab for a line 28 | > `content` - string that contains crontab, one crontab for a line 29 | > `override` - override the origin crontab 30 | 31 | Example: 32 | 33 | ```python 34 | from deploycron import deploycron 35 | 36 | # specify a filenmae 37 | deploycron(filename="/tmp/youcrontab.tab") 38 | 39 | # or just specify crontab content 40 | deploycron(content="* * * * * echo hello > /tmp/hello") 41 | 42 | # if you want to overwrite the existing crontab, set `override` to True 43 | deploycron(content="* * * * * echo hello > /tmp/hello", override=True) 44 | ``` 45 | 46 | ## Note 47 | 48 | Only support in unix-like system, eg. Linux/Mac 49 | 50 | ## Author 51 | 52 | * Monklof (monklof@gmail.com) 53 | 54 | ## License 55 | 56 | MIT 57 | -------------------------------------------------------------------------------- /deploycron/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import subprocess 4 | import os 5 | 6 | def deploycron(filename="", content="", override=False): 7 | """install crontabs into the system if it's not installed. 8 | This will not remove the other crontabs installed in the system if not specified 9 | as override. It just merge the new one with the existing one. 10 | If you provide `filename`, then will install the crontabs in that file 11 | otherwise install crontabs specified in content 12 | 13 | filename - file contains crontab, one crontab for a line 14 | content - string that contains crontab, one crontab for a line 15 | override - override the origin crontab 16 | """ 17 | if not filename and not content: 18 | raise ValueError("neither filename or crontab must be specified") 19 | 20 | if filename: 21 | try: 22 | with open(filename, 'r') as f: 23 | content = f.read() 24 | except Exception as e: 25 | raise ValueError("cannot open the file: %s" % str(e)) 26 | if override: 27 | installed_content = "" 28 | else: 29 | # currently installed crontabs 30 | retcode, err, installed_content = _runcmd("crontab -l") 31 | if retcode != 0 and 'no crontab for' not in err: 32 | raise OSError("crontab not supported in your system") 33 | # merge the new crontab with the old one 34 | installed_content = installed_content.rstrip("\n") 35 | installed_crontabs = installed_content.split("\n") 36 | for crontab in content.split("\n"): 37 | if crontab and crontab not in installed_crontabs: 38 | if not installed_content: 39 | installed_content += crontab 40 | else: 41 | installed_content += "\n%s" % crontab 42 | if installed_content: 43 | installed_content += "\n" 44 | # install back 45 | retcode, err, out = _runcmd("crontab", installed_content) 46 | if retcode != 0: 47 | raise ValueError("failed to install crontab, check if crontab is valid") 48 | 49 | def _runcmd(cmd, input=None): 50 | '''run shell command and return the a tuple of the cmd's return code, std error and std out 51 | WARN: DO NOT RUN COMMANDS THAT NEED TO INTERACT WITH STDIN WITHOUT SPECIFY INPUT, 52 | (eg cat), IT WILL NEVER TERMINATE. 53 | ''' 54 | 55 | if input is not None: 56 | p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, 57 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, 58 | close_fds=True, preexec_fn=os.setsid) 59 | else: 60 | p = subprocess.Popen(cmd, shell=True, 61 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, 62 | close_fds=True, preexec_fn=os.setsid) 63 | 64 | stdoutdata, stderrdata = p.communicate(input) 65 | return p.returncode, stderrdata, stdoutdata 66 | 67 | 68 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from setuptools import setup 3 | 4 | setup( 5 | name = "deploycron", 6 | version = "0.0.1", 7 | author = "monklof", 8 | author_email = "monklof@gmail.com", 9 | description = ("a small crontab deploy/install tool for python"), 10 | license = "MIT", 11 | keywords = "crontab, cron, initialize", 12 | url = "https://github.com/monklof/deploycron", 13 | packages=['deploycron', 'tests'], 14 | test_suite = 'nose.collector', 15 | classifiers=[ 16 | "Development Status :: 2 - Pre-Alpha", 17 | "Topic :: Utilities", 18 | "License :: OSI Approved :: MIT License", 19 | ], 20 | ) 21 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/monklof/deploycron/3d59e4e95edba3c7c5638990a409647d2698b6f5/tests/__init__.py --------------------------------------------------------------------------------