Created
July 12, 2019 02:21
-
-
Save glasslion/91be3117c525a3ea046ad3f64ead87c6 to your computer and use it in GitHub Desktop.
一个把 pip requirements 转换成 poetry依赖的脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import absolute_import | |
import argparse | |
import os | |
import subprocess | |
def normalize(line): | |
line = line.strip('\n') | |
if line.startswith('#') or line.startswith('-'): | |
return | |
if line.strip() == '': | |
return | |
return line | |
def add_package(package, dev=False): | |
print(f'Adding {package} ...') | |
cmd = ['poetry', 'add', package] | |
if dev: | |
cmd.append('-D') | |
p = subprocess.run( | |
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8') | |
print(p.stdout) | |
print(p.stderr) | |
# For now, poetry does not return non zero when add fails, we tell it fails | |
# if stderr is not empty | |
return p.stderr | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Import poetry package dependences from pip requirements.txt') | |
parser.add_argument('file', help='requirements.txt file') | |
parser.add_argument('-D' , '--dev', action='store_true', | |
help='development dependency') | |
args = parser.parse_args() | |
failed = [] | |
abspath = os.path.abspath(args.file) | |
with open(abspath) as req_file: | |
for line in req_file: | |
package = normalize(line) | |
if not package: | |
continue | |
err = add_package(package, args.dev) | |
if err: | |
failed.append((package, err)) | |
print('\n\nFollowing packages are not added due to error:') | |
for package, err in failed: | |
print(f'{package}:\n{err}\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment