This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.py
107 lines (94 loc) · 4.02 KB
/
github.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- python-indent-offset: 4 -*-
#
# @file github.py
# @brief Utilities for dealing with GitHub logins and such.
# @author Michael Hucka
#
# <!---------------------------------------------------------------------------
# Copyright (C) 2015 by the California Institute of Technology.
# This software is part of CASICS, the Comprehensive and Automated Software
# Inventory Creation System. For more information, visit http://casics.org.
# ------------------------------------------------------------------------- -->
from configparser import ConfigParser
import os
import sys
from pymongo import MongoClient
from datetime import datetime
from messages import *
class GitHub():
'''Class for handling GitHub user log-ins.'''
@staticmethod
def login(hosting_service, service_account):
cfg = Config('mongodb.ini')
section = hosting_service
user_name = None
user_password = None
try:
if service_account:
for name, value in cfg.items(section):
if name.startswith('login') and value == service_account:
user_name = service_account
index = name[len('login'):]
if index:
user_password = cfg.get(section, 'password' + index)
else:
# login entry doesn't have an index number.
# Might be a config file in the old format.
user_password = value
break
# If we get here, we failed to find the requested login.
msg('Cannot find "{}" in section {} of config.ini'.format(
service_account, section))
else:
try:
user_name = cfg.get(section, 'login1')
user_password = cfg.get(section, 'password1')
except:
user_name = cfg.get(section, 'login')
user_password = cfg.get(section, 'password')
except Exception as err:
msg(err)
text = 'Failed to read "login" and/or "password" for {}'.format(
section)
raise SystemExit(text)
return (user_name, user_password)
# Configuration file handling
# .............................................................................
class Config():
'''A class to encapsulate reading our configuration file.'''
_default_config_file = os.path.join(os.path.dirname(__file__), "config.ini")
def __init__(self, cfg_file=_default_config_file):
self._cfg = ConfigParser()
try:
with open(cfg_file) as f:
self._cfg.readfp(f)
except IOError:
raise RuntimeError('file "{}" not found'.format(cfg_file))
def get(self, section, prop):
'''Read a property value from the configuration file.
Two forms of the value of argument "section" are understood:
* value of section is an integer => section named after host
* value of section is a string => literal section name
'''
if isinstance(section, str):
return self._cfg.get(section, prop)
elif isinstance(section, int):
section_name = Host.name(section)
if section_name:
return self._cfg.get(section_name, prop)
else:
return None
def items(self, section):
'''Returns a list of tuples of (name, value) for the given section.
Two forms of the value of argument "section" are understood:
* value of section is an integer => section named after host
* value of section is a string => literal section name
'''
if isinstance(section, str):
return self._cfg.items(section)
elif isinstance(section, int):
section_name = Host.name(section)
if section_name:
return self._cfg.items(section_name)
else:
return None