-
Notifications
You must be signed in to change notification settings - Fork 5
/
licenses.py
63 lines (45 loc) · 2.44 KB
/
licenses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import print_function, unicode_literals
import os
import click
import datetime
import requests
import click
import json
import sys
from subprocess import call, STDOUT
def getRequestsAsJSON(fromURL):
r = requests.get(fromURL)
if r.status_code==200:
return r.json()
else:
click.secho("Internal error occured.", bold=True, fg='red')
sys.exit(0)
def getLicenseKey(lics, choice):
return [k for k, v in lics.items() if v == choice][0]
def replacePlaceholders(licenseText, licenseIdentifier, authorName, licenseYear):
if licenseIdentifier == 'gpl-3.0' or licenseIdentifier == 'agpl-3.0':
programName = click.prompt("\nEnter program name")
desc = click.prompt("\nEnter one line to give a brief idea of what the program does")
return licenseText.replace('<year>', str(licenseYear)).replace('<name of author>', authorName).replace('<program>', programName).replace("<one line to give the program's name and a brief idea of what it does.>", (programName + ' ' + desc))
elif licenseIdentifier == 'apache-2.0':
return licenseText.replace('[yyyy]', str(licenseYear)).replace('[name of copyright owner]', authorName)
elif licenseIdentifier == 'gpl-2.0':
return licenseText.replace('Copyright (C) year name of author', ('Copyright (C) ' + str(licenseYear) + ' ' + authorName))
elif licenseIdentifier == 'lgpl-2.1':
libraryName = click.prompt("\nEnter library name")
desc = click.prompt("\nEnter one line to give a brief idea of what the library does")
return licenseText.replace('<year>', str(licenseYear)).replace('<name of author>', authorName).replace("<one line to give the library's name and a brief idea of what it does.>", (libraryName + ' ' + desc))
else:
return licenseText.replace('[year]', str(licenseYear)).replace('[fullname]', authorName)
def createLicense(srcURL, lKey):
licenseBody = getRequestsAsJSON(srcURL + '/' + lKey)['body']
name = click.prompt("\nEnter your name")
year = click.prompt("\nEnter the year(Press enter to use the current year)", show_default = False, default = datetime.datetime.now().year)
licenseBody = replacePlaceholders(licenseBody, lKey, name, year)
licenseFile = open('LICENSE', 'w')
licenseFile.write(licenseBody)
def generateLicense(licenseURL, licenses, chosen):
click.clear()
chosenLicenseKey = getLicenseKey(licenses, chosen)
createLicense(licenseURL, chosenLicenseKey)
click.secho("\nCreated LICENSE file based on the %s in the current directory.\n" %chosen, fg = "green", bold = True)