aboutsummaryrefslogtreecommitdiff
blob: 8bfdf917183c3cb9527aa358ccce84e507949d1f (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Contains functions to import package versions into the database

package repository

import (
	"regexp"
	"soko/pkg/config"
	"soko/pkg/database"
	"soko/pkg/logger"
	"soko/pkg/models"
	"soko/pkg/portage/utils"
	"strings"
)

// isVersion checks whether the path points to a package version
// that is an .ebuild file
func isVersion(path string) bool {
	isVersion, _ := regexp.MatchString(`[^/]*\/[^/]*\/.*\.ebuild`, path)
	return isVersion
}

// UpdateVersion updates the version in the database in case
// the given path points to a package version
func UpdateVersion(path string) {

	line := strings.Split(path, "\t")

	if len(line) != 2 {
		if len(line) == 1 && isVersion(path) {
			updateModifiedVersion(path)
		}
		return
	}

	status := line[0]
	changedFile := line[1]

	if isVersion(changedFile) && status == "D" {
		updateDeletedVersion(changedFile)
	} else if isVersion(changedFile) && (status == "A" || status == "M") {
		updateModifiedVersion(changedFile)
	}
}

// updateDeletedVersion deletes a package version from the database
func updateDeletedVersion(changedFile string) {
	splitted := strings.Split(strings.ReplaceAll(changedFile, ".ebuild", ""), "/")
	category := splitted[0]
	packagename := splitted[1]
	version := strings.ReplaceAll(splitted[2], packagename+"-", "")

	atom := category + "/" + packagename
	id := atom + "-" + version

	versionObject := &models.Version{Id: id}
	_, err := database.DBCon.Model(versionObject).WherePK().Delete()

	if err != nil {
		logger.Error.Println("Error during deleting version " + id)
		logger.Error.Println(err)
	}
}

// updateModifiedVersion adds a package version to the database or
// updates it. To do so, it parses the metadata from the md5-cache
func updateModifiedVersion(changedFile string) {
	splitted := strings.Split(strings.ReplaceAll(changedFile, ".ebuild", ""), "/")
	category := splitted[0]
	packagename := splitted[1]
	version := strings.ReplaceAll(splitted[2], packagename+"-", "")

	atom := category + "/" + packagename
	id := atom + "-" + version

	version_metadata, _ := utils.ReadLines(config.PortDir() + "/metadata/md5-cache/" + id)

	slot := "0"
	subslot := "0"
	eapi := ""
	keywords := ""
	var useflags []string
	var restricts []string
	var properties []string
	var homepages []string
	license := ""
	description := ""

	for _, metadata := range version_metadata {

		switch {
		case strings.HasPrefix(metadata, "EAPI="):
			eapi = strings.ReplaceAll(metadata, "EAPI=", "")

		case strings.HasPrefix(metadata, "KEYWORDS="):
			keywords = strings.ReplaceAll(metadata, "KEYWORDS=", "")

		case strings.HasPrefix(metadata, "IUSE="):
			useflags = strings.Split(strings.ReplaceAll(metadata, "IUSE=", ""), " ")

		case strings.HasPrefix(metadata, "RESTRICT="):
			restricts = strings.Split(strings.ReplaceAll(strings.ReplaceAll(metadata, "RESTRICT=", ""), "!test? ( test )", ""), " ")

		case strings.HasPrefix(metadata, "PROPERTIES="):
			properties = strings.Split(strings.ReplaceAll(metadata, "PROPERTIES=", ""), " ")

		case strings.HasPrefix(metadata, "HOMEPAGE="):
			homepages = strings.Split(strings.ReplaceAll(metadata, "HOMEPAGE=", ""), " ")

		case strings.HasPrefix(metadata, "LICENSE="):
			license = strings.ReplaceAll(metadata, "LICENSE=", "")

		case strings.HasPrefix(metadata, "DESCRIPTION="):
			description = strings.ReplaceAll(metadata, "DESCRIPTION=", "")

		case strings.HasPrefix(metadata, "SLOT="):
			rawslot := strings.ReplaceAll(metadata, "SLOT=", "")
			slot = strings.Split(rawslot, "/")[0]
			if len(strings.Split(rawslot, "/")) > 1 {
				subslot = strings.Split(rawslot, "/")[1]
			}
		}

	}

	ebuildVersion := &models.Version{
		Id:          id,
		Category:    category,
		Package:     packagename,
		Atom:        atom,
		Version:     version,
		Slot:        slot,
		Subslot:     subslot,
		EAPI:        eapi,
		Keywords:    keywords,
		Useflags:    useflags,
		Restricts:   restricts,
		Properties:  properties,
		Homepage:    homepages,
		License:     license,
		Description: description,
	}

	_, err := database.DBCon.Model(ebuildVersion).OnConflict("(id) DO UPDATE").Insert()

	if err != nil {
		logger.Error.Println("Error during updating version " + id)
		logger.Error.Println(err)
	}
}