license.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from subprocess import check_call
  2. import sys
  3. import os
  4. import os.path as op
  5. import shutil as sh
  6. from glob import glob
  7. import argparse
  8. import string
  9. DESCRIPTION = ("Check whether a license exists, and add one if desired.")
  10. CC_BY_SA_LCENSE = ["# License for this book",
  11. "",
  12. "All content in this book (ie, any files and content in the `content/` folder)",
  13. "is licensed under the",
  14. "[Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/) (CC BY-SA 4.0) license."]
  15. parser = argparse.ArgumentParser(description=DESCRIPTION)
  16. parser.add_argument("--path", default=None, help="Path to the folder where you'll check for a license.")
  17. parser.add_argument("--use-license", default=None, help="A pre-defined input for the user-prompt")
  18. if __name__ == '__main__':
  19. args = parser.parse_args()
  20. path = args.path
  21. use_license = args.use_license
  22. path_license = op.join(path, 'LICENSE.md')
  23. if op.exists(path_license):
  24. print("It looks like you've already specified a license for this book, good job!")
  25. sys.exit()
  26. print("\n\nWe noticed you don't have a license for this book. Licenses help \n"
  27. "others (re)use your work, and make it clear which rights you wish to \n"
  28. "retain for your work. We recommend the CC-BY-SA license, a permissive \n"
  29. "and open license that requires attribution to the original author. \n"
  30. "Would you like to apply this license to your book?\n\n")
  31. if use_license is None:
  32. use_license = input("Would you like to use the CC-BY-SA license? (yes)/no: ") or "yes"
  33. if use_license == "yes":
  34. with open(path_license, 'w') as ff:
  35. print("\nCreating a CC-BY-SA license file...\n")
  36. ff.write('\n'.join(CC_BY_SA_LCENSE))
  37. elif use_license == "no":
  38. print("\n\nYou've decided not to use the CC BY-SA license. We've added an empty \n"
  39. "'LICENSE.md file to your `content/` folder so that this message doesn't come \n"
  40. "up again. We recommend filling this file with language that specifies what \n"
  41. "license applies to your book. Check out the Creative \n"
  42. "Commons licenses for several options: https://creativecommons.org/licenses\n\n")
  43. with open(path_license, 'w') as ff:
  44. pass
  45. else:
  46. raise ValueError("Please choose 'yes' or 'no', you chose: '{}'".format(use_license))