Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2017 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of ci-scripts. 

5 

6ci-scripts is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11ci-scripts is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with ci-scripts. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import sys 

22import argparse 

23from typing import Dict, List, Union 

24 

25 

26def parse_args() -> Dict[str, Union[str, List[Dict[str, str]]]]: 

27 """ 

28 Parses command line arguments 

29 :return: A dictionary containing data required for uploading a release 

30 """ 

31 

32 parser = argparse.ArgumentParser() 

33 

34 parser.add_argument("tag_name", 

35 help="The Tag name on which to base the release on") 

36 parser.add_argument("release_notes", 

37 help="The Release Notes. Can be a file or a string") 

38 parser.add_argument("release_assets", 

39 help="The release assets.", nargs="*", default=[]) 

40 parser.add_argument("-b", "--branch", default="master", 

41 help="The source branch or commit on which to base \ 

42 this release on") 

43 

44 args = parser.parse_args() 

45 

46 notes = args.release_notes 

47 if os.path.isfile(notes): 

48 with open(notes, 'r') as release_notes: 

49 notes = release_notes.read() 

50 

51 # Add files in artifacts directory to release assets 

52 all_asset_paths = args.release_assets 

53 if os.path.isdir("artifacts"): 

54 for child in os.listdir("artifacts"): 

55 child_path = os.path.join("artifacts", child) 

56 if os.path.isfile(child_path): 

57 all_asset_paths.append(child_path) 

58 

59 assets = [] 

60 for asset in all_asset_paths: 

61 if not os.path.isfile(asset): 

62 print(asset + " does not exist") 

63 sys.exit(1) 

64 else: 

65 assets.append({ 

66 "path": asset, 

67 "content_type": get_content_type(os.path.basename(asset)) 

68 }) 

69 

70 return { 

71 "tag_name": args.tag_name.strip(), 

72 "notes": notes, 

73 "assets": assets, 

74 "branch": args.branch 

75 } 

76 

77 

78def get_content_type(filename: str) -> str: 

79 """ 

80 Retrieves a sensible content type for a file 

81 :param filename: The name of the file 

82 :return: The content type of the file 

83 """ 

84 

85 try: 

86 extension = filename.rsplit(".", 1)[1].lower() 

87 

88 if extension == "jar": 

89 return "application/java-archive" 

90 elif extension == "txt": 

91 return "text/plain" 

92 else: 

93 return "application/octet-stream" 

94 

95 except IndexError: 

96 return "application/octet-stream"