```python
import pkg_resources
def get_pkg_license(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except:
lines = pkg.get_metadata_lines('PKG-INFO')
license = 'UNKNOWN'
labels = ['License: ', 'Classifier: License :: OSI Approved :: ']
for label in labels:
for line in lines:
if line.startswith(label):
license = line[len(label):]
break
if license != 'UNKNOWN':
return license
return '(Licence not found)'
def get_pkg_home_page(pkg):
try:
lines = pkg.get_metadata_lines('METADATA')
except:
lines = pkg.get_metadata_lines('PKG-INFO')
label = 'Home-page: '
for line in lines:
if line.startswith(label):
url = line[len(label):]
break
if url == 'UNKNOWN':
url = '
https://pypi.python.org/pypi/'+pkg.key return url
def create_packages_and_licenses_text():
text = ''
for pkg in sorted(pkg_resources.working_set, key=lambda x: str(x).lower()):
text += '\t'.join([pkg.key,pkg.version,get_pkg_license(pkg),get_pkg_home_page(pkg)]) + '\n'
return text
if __name__ == "__main__":
text = create_packages_and_licenses_text()
print(text)
filename = 'packages_and_licenses.txt'
with open(filename, 'w', encoding='Shift-JIS') as f:
f.write(text)
```